/**
  ******************************************************************************
  * @file    DCDC_Globals.c
  * @author  STMicroelectronics
  * @version 1.0.0
  * @date    31-Mar-2022
  * @brief   This file contains all global variables for the application
  ******************************************************************************
  *
  * Copyright (c) 2022 Biricha Digital Power Limited
  * Copyright (c) 2014(-2022) STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file in
  * the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  ******************************************************************************
  *
  */

/** @addtogroup STM32_DPOW
  * @brief  Digital Power application implemented with a STM32 MCU
  * @{
  */

/** @addtogroup DCDC_global_variables
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include "DCDC_type.h"
#include "DPC_Miscellaneous.h"
#include "dpwr_pfc.h"
#include "dcdc_globals.h"
#include "app_X-CUBE-DPower.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* exported variables --------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* External variables --------------------------------------------------------*/
DCDC_MeasureStruct_t DCDC_MeasureStruct;        /**< Acquisition struct */

/* Filtered measures */
uint16_t hTemperatureFiltered;                  /**< Temperature filtered measure */
uint16_t hViperVoltageFiltered;                 /**< Viper voltage filtered measure -  NOT USED */
uint16_t hVoutVoltageFiltered;                  /**< Vout voltage filtered measure */
float fVrmsVoltageFiltered;                     /**< Vrms input voltage filtered measure */

volatile float fTemperatureDeg;                 /**< LM19CIZ Temperature in deg */

/* Control variables */
volatile uint16_t hVout_Reference;              /**< Voltage out reference */
volatile uint16_t hVout_Reference_init;         /**< Voltage out reference init value */
//volatile uint16_t hVout_Reference_increment;  /**< Voltage out reference increment */

/* Start-up counters */
volatile uint16_t hDCDC_StartUp_TimeLeft = 0;		           /**< counter after init start-up DC/C */
volatile uint16_t hDCDC_Wait_TimeLeft = 0;                     /**< counter after enter in WAIT state */
volatile uint16_t hDCDC_UndervoltageValidation_TimeLeft = 0;   /**< counter to validate undervoltage protection */
volatile uint16_t hCommunicationTimeLeft = 0;                  /**< counter between two USART communications */
volatile uint16_t hLEDTaskTimeLeft = 0;			               /**< counter between two LED tasks */

/* State variables of converter */
//volatile DCDC_State_t DCDC_State;                                         /**< DCDC state variable */
//volatile uint16_t DCDC_System_Fault = (uint16_t)DCDC_NO_ERROR;            /**< fault variable */
//volatile uint16_t DCDC_Previous_System_Fault = (uint16_t)DCDC_NO_ERROR;   /**< previous DC/DC fault variable */
//volatile uint16_t DCDC_Last_System_Fault = (uint16_t)DCDC_NO_ERROR;		/**< last fault variable value before stop application */
volatile uint16_t PFC_Received_Status = (uint16_t)PFC_BUS_UNDER_VOLT;       /**< last PFC received status */
volatile uint16_t PFC_Last_System_Fault = (uint16_t)PFC_BUS_UNDER_VOLT;	    /**< last PFC fault variable */
#if (FAN_USAGE == true)
volatile uint16_t FAN_PWMDutyCycle;                                         /**< PWM duty cycle for fan control */
#endif

/* Boolean variables */
volatile bool bDCDC_OutputEnabled = false;              /**< true when main PWMs of DCDC are enabled, false otherwise */
#if (SYNCH_RECTIFICATION == true)
volatile bool bDCDC_SynchRectEnabled = false;           /**< true when PWMs of Synch. Rect. are enabled, false otherwise */
#endif
#if (ACTIVE_CLAMP == true)
volatile bool bDCDC_ActiveClampEnabled = false;         /**< true when PWMs Active Clamp are enabled, false otherwise */
#endif
volatile bool bCheckUndervoltageEnabled = false;        /**< true when low out voltage short circuit fault is enabled, false otherwise */
volatile bool bCheckOvertemperatureEnabled = false;     /**< true when overtemperature fault is enabled, false otherwise */
volatile bool bUpdateControlParamEnabled = false;       /**< true when automatic update control parameters is enabled, false otherwise */
#if (FAN_USAGE == true)
volatile bool bDCDC_FanEnabled = false;                 /**< true when Fan is enabled, false otherwise */
#endif
volatile bool bFirstFaultDetected = false;              /**< true when the first fault to show with LED blinking was found, false otherwise */
volatile bool bNonRecoverableError = false;             /**< true when the fault is Not recoverable, false otherwise */

/* Burst mode */
volatile bool bVoutBurstModeEnabled = false;            /**< true when out voltage burst mode is active (Vbus> bm_threshold with hysteresis), false otherwise */

SystStatus_key Key_USER_State;
volatile bool bDCDC_SecondarySynchRectEnabled = false;  /**< true when PWMs of Synch. Rect. are enabled, false otherwise */

/* External functions ---------------------------------------------------------*/

/**
  * @} end DCDC_global_variables
  */

/**
  * @} end STM32_DPOW
  */
